<?php

namespace app\common\model;

use think\Model;

class OrderRemind extends Model
{
    // 表名
    protected $name = 'order_remind';
	
	// 自动写入时间戳字段
	protected $autoWriteTimestamp = true;
	
	
	// 追加属性
	protected $append = [
		'create_time_text',
		'status_text',
		'update_time_text'
	];
	
	const STATUS_W = 0;
	const STATUS_Y = 1;
	
	public function getStatusList()
	{
		return [self::STATUS_W => '未读', self::STATUS_Y => '已读'];
	}
	
	
	public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	
	public function user() {
		return $this->belongsTo('User', 'user_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function order() {
		return $this->belongsTo('Order', 'order_id', 'id', '', 'left')->setEagerlyType(0);
	}
	
	public function business() {
		return $this->belongsTo('business', 'business_id', 'id', '', 'left')->setEagerlyType(0);
	}
}
